Lab5
4531208021_4531209621 นาย ฐณวัฒน์ คำนูณเศรษฐ์ และ นาย ชาตา ซาลวาลา (13/8/2545 (11:20:30))
(SM=1, CM=17, ST=20, KY=0, TR=22:16)
TestScript |
Mini-Quiz : (0.0 คะแนน)
JLab>javac Lab5.java
JLab>
JLab>java Selftest
>>JLabIO->Testing (2012, 6) : 2.00
>>JLabIO->Testing (2008, 7) : 2.00
>>JLabIO->Testing (1994, 8) : 2.00
>>JLabIO->Testing (1993, 12) : 2.00
>>JLabIO->Testing (2024, 9) : 2.00
>>JLab:<POINT>10.00</POINT>
JLab>
ได้ 10 คะแนน
|
Source Code |
/**
* 2110101 Computer Programming
* Lab #5 : Loops : while, do-while, for
*/
import java.util.*;
import jlab.JLabIO;
public class Lab5 {
public static void calendar(int year, int month) {
// Example
//-----------------------------
// August(2002)
//-----------------------------
// SUN MON TUE WED THU FRI SAT
// 1 2 3 \
// 4 5 6 7 8 9 10 |
// 11 12 13 14 15 16 17 > your code
// 18 19 20 21 22 23 24 |
// 25 26 27 28 29 30 31 /
//=============================
// print header
System.out.println(LONGLINE);
System.out.println(" " + monthName[month - 1] + "(" + year + ")");
System.out.println(LONGLINE);
System.out.println(" SUN MON TUE WED THU FRI SAT");
Calendar calendar = new GregorianCalendar(new Locale("th", "TH"));
calendar.set((year > 2500 ? year - 543 : year), month - 1, 1);
int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
int numDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
// add your code here to format and print the month
// Use the following two variables :
// - numDays is the number of days in this month
// - firstDay is the day-of-week of the first of this month
int j = firstDay;
for (int k = 1; k < firstDay; k++) {
System.out.print(" ");
}
for (int i = 1; i <= numDays; i++, j++) {
System.out.print(JLabIO.format(i, 4));
if (j % 7 == 0) System.out.println();
}
System.out.println();
// add a line to notify the end of printing
System.out.println(LASTLINE);
}
//----------------------------------------------------------------
public static void main(String[] args) {
int y = JLabIO.readInt("Year = ");
int m = JLabIO.readInt("Month = ");
if (y > 0 && (1 <= m && m <= 12)) {
calendar(y, m);
} else {
System.out.println("error : invalid year or month");
}
}
//----------------------------------------------------------------
final static String[] monthName =
{"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
final static String LONGLINE = "-----------------------------";
final static String LASTLINE = "=============================";
}
|
|